<?php

class PermitidosController extends AppController {

    public $layout = 'correspondencia';
    public $uses = array('User', 'Permitido');

    public function index() {
        $permitidos = $this->Permitido->find('all', array('recursive' => -1, 'limit' => 50, 'order' => array('Permitido.id DESC')));
        $this->set(compact('permitidos'));
    }

    public function insertar($idUser = null) {

        if (!empty($this->request->data)) {
            $idD=$this->request->data['Permitido']['destino_id'];
            $verifica=  $this->Permitido->find('first',array('conditions'=>array('Permitido.destino_id'=>$idD)));
            if(empty($verifica)){
               $this->Permitido->create();
               $this->request->data['Permitido']['estado']=Activo;
               //debug($this->request->data);
            if ($this->Permitido->save($this->request->data)) {
                $this->Session->setFlash('Guardado correctamente','msgbueno');
                $this->redirect(array('action' => 'insertar', $idUser));
            } else {
                $this->Session->setFlash('no se guardo', msgerror);
            }  
            }else{
                $this->Session->setFlash('ya existe','msgerror');
            }
           
        }
        $destino = $this->User->find('list', array('fields' => 'User.nombre'));
        $this->set(compact('destino'));

        $usuarios = $this->User->find('first', array(
            'recursive' => -1,
            'conditions' => array('User.id' => $idUser)
        ));
        //debug($usuarios);
        
        $permitidos = $this->Permitido->find('all', array(
             'recursive'=>0,
             'conditions'=>array('Permitido.user_id'=>$idUser)
        ));
        
        //debug($permitidos);die;
        $this->set(compact('usuarios', 'permitidos','idUser'));
    }

    public function asignacion() {
       
             //debug($this->request->is('post')); die;
            $destino = 3;
            //debug($destino);
            die;
            $des=  $this->Permitido->find('first',array(
                'recursive'=>-1,));
        
    }

    public function editar($id = null) {
        $this->Permitido->id = $id;
        if (!$this->Permitido->exists()) {
            throw new NotFoundException(__('Invalido'));
        }
        if (!empty($this->request->data)) {
            //debug($this->request->data);exit;
            $this->Permitido->id = $id;
            if ($this->Permitido->save($this->request->data)) {
                $this->Session->setFlash('se guardo correctamente');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error. Intentelo de nuevo');
                $this->redirect(array('action' => 'index'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
        }
    }

    public function eliminar($id=null,$idUser=null) {
        if ($this->request->data('post')) {
            throw new MethodNotAllowedException();
        }
        if ($this->Permitido->delete($id)) {
            $this->Session->setFlash('Eliminado correctamente','msgbueno');
            $this->redirect(array('controller'=>'permitidos','action' => 'insertar',$idUser));
        }
    }
    public function alta($id=null,$idUser=null){
        $this->Permitido->id=$id;
        $this->request->data['Permitido']['estado']=Activo;
        //debug($this->request->data);die;
        if($this->Permitido->save($this->request->data))
        {
            $this->Session->setFlash('Se dio Alta', 'msgbueno');
            $this->redirect(array('controller'=>'Permitidos','action'=>'insertar',$idUser));
        }
    }
    public function baja($id=null,$idUser=null){
        $this->Permitido->id=$id;
        $this->request->data['Permitido']['estado']=Baja;
        //debug($this->request->data);die;
        if($this->Permitido->save($this->request->data))
        {
            $this->Session->setFlash('se dio Baja', 'msgbueno');
            $this->redirect(array('controller'=>'Permitidos', 'action'=>'insertar',$idUser));
        }
    }

}

?>
